<template>
  <div class="UserInfo">
    <div class="loading" v-if="isLoading">
      <img src="../assets/loading.gif" />
    </div>
    <div class="userInfomation" v-else>
      <section>
        <img :src="userinfo.avatar_url" />
        <span>{{ userinfo.loginname }}</span>
        <p>{{ userinfo.score }}积分</p>
        <p>注册于: {{ userinfo.create_at | formatDate }}</p>
      </section>
      <section class="replies">
        <p>回复</p>
        <ul>
          <li v-for="(item, index) in userinfo.recent_replies" :key="index">
            <router-link
              :to="{
                name: 'post_content',
                params: {
                  id: item.id,
                  name: item.name,
                },
              }"
            >
              {{ item.title }}
            </router-link>
          </li>
        </ul>
      </section>
      <section class="topics">
        <p>创建</p>
        <ul>
          <li v-for="(item, title) in userinfo.recent_topics" :key="title">
            <router-link
              :to="{
                name: 'post_content',
                params: {
                  id: item.id,
                  name: item.name,
                },
              }"
            >
              {{ item.title }}
            </router-link>
          </li>
        </ul>
      </section>
    </div>
  </div>
</template>

<script>
export default {
  name: "UserInfo",
  data() {
    return {
      isLoading: false,
      userinfo: {},
    };
  },
  methods: {
    getUserData() {
      this.$http
        .get(`https://cnodejs.org/api/v1/user/${this.$route.params.name}`)
        .then((res) => {
          if (res.data.success === true) {
            this.isLoading = false;
            this.userinfo = res.data.data;
          }
        })
        .catch((err) => console.log(err));
    },
  },
  beforeMount() {
    this.isLoading = true;
    this.getUserData();
  },
};
</script>

<style scoped>
.userInfomation {
  background: white;
  width: 75%;
  margin: 10px auto;
}
.userInfomation section {
  padding: 12px;
}
.userInfomation img {
  width: 30px;
}
.userInfomation li {
  list-style: none;
}
.userInfomation .replies,
.userInfomation .topics {
  font-size: 0.72rem;
  border-top: 10px #dddddd solid;
}
.userInfomation > section > p {
  padding: 12px 0 12px 12px;
  background-color: rgba(212, 205, 205, 0.17);
  font-size: 0.75rem;
  margin: 0;
}
.userInfomation > section > ul > li {
  padding: 4px 0 4px 12px;
  white-space: nowrap;
  font-size: 0.72rem;
  text-overflow: ellipsis;
  overflow: hidden;
  line-height: 30px;
  vertical-align: middle;
}
.userInfomation > section > ul > li > a {
  color: #094e99;
  text-decoration: none;
}
</style>